import { FC, useReducer } from 'react';
type StateType = { count: number };
type ActionType = { type: string };
const initialState: StateType = { count: 100 };
function reducer(state: StateType, action: ActionType) {
  switch (action.type) {
    case 'increment':
      return { count: state.count + 1 };
    case 'decrement':
      return { count: state.count - 1 };
    default:
      throw new Error('action type error');
  }
}

const CountReducer: FC = () => {
  const [state, dispatch] = useReducer(reducer, initialState);
  function add() {
    dispatch({ type: 'increment' });
  }
  function reduce() {
    dispatch({ type: 'decrement' });
  }
  return (
    <>
      <span>value: {state.count}</span>
      <button onClick={add}>+</button>
      <button onClick={reduce}>-</button>
    </>
  );
};

export default CountReducer;
